Android手机文件系统操作——轻松存储与获取手机文件

本文介绍了Android系统中获取各种路径的方法,包括通过Environment和Context获取内部存储、缓存、SD卡、数据库、文件等路径。同时提供了一个完整的操作手机文件的工具类,帮助开发者轻松进行文件存储和获取。
摘要由CSDN通过智能技术生成

Android获取各种系统路径的方法

通过Environment获取的

  • Environment.getDataDirectory().getPath() :                                      获得根目录/data 内部存储路径
  • Environment.getDownloadCacheDirectory().getPath()  :               获得缓存目录/cache
  • Environment.getExternalStorageDirectory().getPath():                  获得SD卡目录/mnt/sdcard(获取的是手机外置sd卡的路径)
  • Environment.getRootDirectory().getPath() :                                     获得系统目录/system

通过Context获取的

  • Context.getDatabasePath()                                返回通过Context.openOrCreateDatabase 创建的数据库文件
  • Context.getCacheDir().getPath() :          用于获取APP的cache目录/data/data//cache目录

  • Context.getExternalCacheDir().getPath()  :                           用于获取APP的在SD卡中的cache目录/mnt/sdcard/android/data//cache

  • Context.getFilesDir().getPath()  :          用于获取APP的files目录 /data/data//files

  • Context.getObbDir().getPath():              用于获取APPSDK中的obb目录/mnt/sdcard/Android/obb/

  • Context.getPackageName() :                                                  用于获取APP的所在包目录

  • Context.getPackageCodePath()  :                                          来获得当前应用程序对应的 apk 文件的路径
  • Context.getPackageResourcePath() :                                   获取该程序的安装包路径

完整 操作手机文件 工具类:


    public void saveToPhone(View v) {
//        FileDir();
        SDCardTest();
    }

    private void FileDir() {
        boolean mExternalStorageAvailable = false;
        boolean mExternalStorageWriteable = false;
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
        // We can read and write the media
            mExternalStorageAvailable = mExternalStorageWriteable = true;
        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            // We can only read the media
            mExternalStorageAvailable = true;
            mExternalStorageWriteable = false;
        } else {
            // Something else is wrong. It may be one of many other states, but all we need
            //  to know is we can neither read nor write
            mExternalStorageAvailable = mExternalStorageWriteable = false;
        }


        Log.i("codecraeer", "getFilesDir = " + getFilesDir());
        Log.i("codecraeer", "getExternalFilesDir = " + getExternalFilesDir("exter
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值